import React, { Component } from 'react';
import { base, toast, getMultiLang } from 'nc-lightapp-front';
import ReferLoader from '../../../../public/ReferLoader/index.js';
import { accbookRefcode } from '../../../../public/ReferLoader/constants';
const { NCModal, NCButton } = base;
import './index.less'

export default class BatchTransferModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			accountbooks: [],
			show: false,
			json: {}
		};
	}
	componentWillMount() {
		let callback= (json) =>{
			this.setState({
				json:json,
			})
		}
		getMultiLang({moduleId:'20020SDTREPAGE',domainName:'gl',currentLocale:'simpchn',callback});
	}

	componentWillReceiveProps(nextProps) {
		let { show, defaultValue } = nextProps;
		let accountbooks = defaultValue || [];
		this.setState({ show, accountbooks });
	}
	componentDidMount(){
		this.props.BatchTransferModalRef(this);
	}
	show = () => {
		this.props.modal.show('BatchTransferModal');
	}
	close = () => {
		this.props.modal.close('BatchTransferModal');
	}
	handleConfirm = () => {
		let { accountbooks } = this.state;
		if(accountbooks.length===0){
			toast({ content: this.state.json['20020SDTREPAGE-000000'], color: 'warning'});/* 国际化处理： 请选择核算账簿！*/
		} else{
			this.props.handleBatchTransferOK(accountbooks);
		}
	};

	renderReferLoader = () => {
		let { accountbooks, json } = this.state;
		let { context } = this.props;
		return(
			<div className='batchTransferModal-body' fieldid='batchExec'>
				<i className='require-true'>*</i>
				<div className='item-title'>{json['20020SDTREPAGE-000001']}:</div>
				{/* 国际化处理： 请选择核算账簿*/}
				<div className='item-refer'>
					<ReferLoader
						tag="batchExec"
						fieldid = 'accountbooks'
						placeholder={json['20020SDTREPAGE-000001']}/* 国际化处理： 请选择核算账簿*/
						refcode={accbookRefcode}
						value={accountbooks}
						isMultiSelectedEnabled={true}
						showGroup={false}
						showInCludeChildren={true}
						isHasDisabledData = {false}
						queryCondition={() => {
							let condition = {
								TreeRefActionExt: 'nccloud.web.gl.ref.AccountBookRefSqlBuilder',
								appcode: '',
								filterSecondBU:true,
								filterProfitCenter:true
							};
							condition.appcode = context.appcode;
							return condition;
						}}
						onChange={(v) => {
							accountbooks = v;
							this.setState({ accountbooks });
						}}
					/>
				</div>
			</div>
		)
	}
	render() {
		let { modal } = this.props;
		return (
			<div>
				{modal.createModal('BatchTransferModal', {
					title: this.state.json['20020SDTREPAGE-000002'], /* 国际化处理： 批量结转*/
					type:'senior',
					className: "batchTransferModal",
					content: this.renderReferLoader(), //弹框内容，可以是字符串或dom
					beSureBtnClick: this.handleConfirm,//点击确定按钮事件
				})}
			</div>
		);
	}
}
